<?php
namespace App\Http\Controllers\Admin;
// use Illuminate\Http\Request;
class Common {
	public static function getTree($pInfo, $spread = true){
	    $res = [];
	    $tree = [];
	    //整理数组
	    foreach($pInfo as $key=>$vo){
	        if($spread){
	            $vo['spread'] = true;  //默认展开
	        }
	        $res[$vo['id']] = $vo;
	        $res[$vo['id']]['children'] = [];
	    }
	    unset($pInfo);
	    //查找子孙
	    foreach($res as $key=>$vo){
	        if(0 != $vo['pid']){
	            $res[$vo['pid']]['children'][] = &$res[$key];
	        }
	    }
	    //过滤杂质
	    foreach( $res as $key=>$vo ){
	        if(0 == $vo['pid']){
	            $tree[] = $vo;
	        }
	    }
	    unset( $res );
	    return $tree;
	}
}
